package jsyf.oa.service.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.system.CompanyDao;
import jsyf.oa.dao.system.DepartDao;
import jsyf.oa.dao.system.Role2DepartDao;
import jsyf.oa.entity.system.Role2DepartEntity;
import jsyf.oa.facade.system.Role2DepartFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jjx on 2018/5/27.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class Role2DepartService implements Role2DepartFacade {

    @Autowired
    private Role2DepartDao role2DepartDao;

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    private DepartDao departDao;

    @Override
    public boolean save(String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);

        role2DepartDao.removeByCondition(" AND ROLE_NAME_ID = ? ",new String[]{jsonParam.getString("roleNameId")});
        if(!StringUtils.isEmpty(jsonParam.getString("departIds"))){
            role2DepartDao.insertBatch(jsonParam.getString("roleNameId"),jsonParam.getString("departIds"));
        }
        return true;
    }

    @Override
    public List<JSONObject> getTreeNodes() {
        List<JSONObject> nodeList = new ArrayList<>();
        List<JSONObject> companyList = companyDao.getJSONListByCondition(" AND DEL_FLAG = 0  ORDER BY SORT_NO", new Object[]{});
        for (int i = 0; i < companyList.size(); i++) {
            JSONObject node = companyList.get(i);
            if (StringUtils.isEmpty(node.getString("parentId")) || "0".equals(node.getString("parentId"))) {
                node.put("pId", "0");
            } else {
                node.put("pId", node.getString("parentId"));
            }
            node.put("type", "company");
            node.put("companyId", node.getString("id"));
            node.put("isParent", true);
            nodeList.add(node);
        }
        List<JSONObject> list = departDao.getJSONListByCondition("and DEL_FLAG=0 ORDER BY SORT_NO", new Object[]{});
        for (int i = 0; i < list.size(); i++) {
            JSONObject node = list.get(i);
            if (StringUtils.isEmpty(node.getString("parentId")) || "0".equals(node.getString("parentId"))) {
                node.put("pId", node.getString("companyId"));
            } else {
                node.put("pId", node.getString("parentId"));
            }
            node.put("companyId", node.getString("companyId"));
            node.put("type", "depart");
            nodeList.add(node);
        }
        return nodeList;
    }

    @Override
    public List<JSONObject> getByRoleNameId(String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        List<JSONObject> reulst=role2DepartDao.getJSONListByCondition(" AND ROLE_NAME_ID = ? ",new Object[]{jsonParam.getString("roleNameId")});
        if (reulst!=null&&reulst.size()>0){
           return reulst;
        }
        return null;
    }


}
